/******************************************************************
 * Created by: DO HOANG HAI                                       
 * Date of Creation: 25/Aug/2007                                    
 * Date of last modification: 25th/Dec/2007 
 *                                                                
 * Description: Provide methods to access/manipulate the FileInfo
 * data of items in the database.
 * 
 * Status: Optimized & Finished
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using Pbdms.Common.DataObject;
using Pbdms.DataAccess.DataProvider;
using System.Collections;

namespace Pbdms.BusinessLogic.DataController
{
    /// <summary>
    /// Provide methods to access/manipulate the FileInfo data of items in the database.
    /// </summary>
    public class FileInfoController
    {
        public FileInfoController()
        {
        }
        /// <summary>
        /// create an instance
        /// </summary>
        /// <returns>an instance of FileInfoController class</returns>
        public static FileInfoController create()
        {
            return new FileInfoController();
        }
        /// <summary>
        /// get FileInfo object of the given item (by Id)
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public FileInfo getFileInfoObject(String itemId)
        {
            return getFileInfoFromDB(itemId);
        }
        /// <summary>
        /// delete fileInfo data from the database of the item with given itemId
        /// </summary>
        /// <param name="itemObjId"></param>
        public void deleteFileInfo(String itemObjId)
        {
            FileInfoProvider.create(itemObjId).deleteDB();
        }

        /// <summary>
        /// update the fileInfo object to database
        /// </summary>
        /// <param name="fileInfoObj"></param>
        public void updateFileInfoToDB(FileInfo fileInfoObj)
        {
            if (fileInfoObj.Location.Trim().Equals(Common.Global.startupPath() + "\\Library"))
                fileInfoObj.Location = "";
            FileInfoProvider.create(fileInfoObj).updateAllToDB();
        }

        /// <summary>
        /// get a fileInfo object from the database
        /// </summary>
        /// <param name="itemID"></param>
        /// <returns></returns>
        internal FileInfo getFileInfoFromDB(string itemID)
        {           
            FileInfo fileInfoObj = FileInfoProvider.create(itemID).selectFromDB();            
            if (fileInfoObj.Location.Trim().Equals(""))
                fileInfoObj.Location = Common.Global.startupPath()+"\\Library";
            return fileInfoObj;
        }

        /// <summary>
        /// get list of URLs of files of all items in the database
        /// </summary>
        /// <param name="arlItemIDs">ArrayList of all items'id</param>
        /// <returns></returns>
        public ArrayList getListOfURLs(ArrayList arlItemIDs)
        {
            if (arlItemIDs != null && arlItemIDs.Count > 0)
            {
                ArrayList arlResult = new ArrayList();
                foreach (object obj in arlItemIDs)
                {
                    String itemId = (string)obj;
                    FileInfo fileInfoObj = FileInfoController.create().getFileInfoFromDB(itemId);
                    arlResult.Add(fileInfoObj.Location +"\\"+ fileInfoObj.FileName);
                }
                return arlResult;
            }
            else
                return null;
        }
        /// <summary>
        /// get arrayList of FileInfo Object of a given arraylist of itemIds
        /// </summary>
        /// <param name="arlItemIDs"></param>
        /// <returns></returns>
        public ArrayList getListFileInfoObjs(ArrayList arlItemIDs)
        {
            if (arlItemIDs != null && arlItemIDs.Count > 0)
            {
                ArrayList arlResult = new ArrayList();
                foreach (object obj in arlItemIDs)
                {
                    String itemId = (string)obj;
                    FileInfo fileInfoObj = FileInfoController.create().getFileInfoFromDB(itemId);                    
                }
                return arlResult;
            }
            else
                return null;


        }

        /// <summary>
        /// append a file info object to the database
        /// </summary>
        /// <param name="fileInfoObj"></param>
        public void appendFileInfo(FileInfo fileInfoObj)
        {
            FileInfoProvider.appendFileInfo(fileInfoObj);
        }
    }
}
